package cpr.data;

import java.util.LinkedList;
import java.util.List;

import cpr.Rule;
import cpr.Utils;
import cpr.context.SolveContext;

public class SolveCandidates2 {
    public LinkedList listOfRuleNr;
    public int cursor = 0;
    public SolveContext ctx;
    public SolveCandidates2(int size, SolveContext ctx) {
    	this.ctx = ctx;
        this.listOfRuleNr = new LinkedList();
        for (int i = 0; i < size; i++) {
        	listOfRuleNr.add(new Integer(i));
		}
        cursor = 0;
    }
    int next = -1;
    
    public AR getAR(int nextRuleNr, int baseScore){
    	AR nextAR = null;
            
		Rule aNextRule = (Rule)ctx.hl_rules.get(nextRuleNr);
		nextAR = ctx.computeAR(aNextRule, ctx.hl_rules, ctx.hl_vectors, baseScore,ctx);
		Utils.computeARentriesList(nextAR);
    	return nextAR;
    }
    public void setNext(int ruleNr, boolean force) {
    	setNext(ruleNr,cursor, force);
    }
    public void setNext(int ruleNr) {
    	setNext(ruleNr,cursor, false);
    }
    public void setNext(int ruleNr,int setTo, boolean force) {
       	int targetIndex  = listOfRuleNr.indexOf(new Integer(ruleNr));
        if (targetIndex==-1) {
            return;
        }
        Integer target = (Integer)listOfRuleNr.get(targetIndex);
        if (targetIndex==setTo) {
            // do nothing
        } else  if (targetIndex<setTo) {
            //add the same point again...

        	listOfRuleNr.remove(targetIndex);
            //int newIndex = setTo-1;
            listOfRuleNr.add(setTo, target);
            //setTo--;
            return;
        } else {
        	
            int newIndex = (targetIndex + setTo)/2;
            if (force) {
            	newIndex = setTo;
            }
            listOfRuleNr.remove(targetIndex);
            listOfRuleNr.add(newIndex, target);
        }
    }
    
    
    public int getNext() {
        Integer a = (Integer)listOfRuleNr.get(cursor);
        cursor++;
        return a.intValue();
    }
   
}
